package com.my.xq.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.my.xq.domain.CourtRoleBindQueryParam;
import com.my.xq.domain.RoleAppointParam;
import com.my.xq.domain.YwhSearchParam;
import com.my.xq.domain.entity.CourtRole;
import com.my.xq.domain.entity.CourtRole_Proprietor;
import com.my.xq.domain.entity.Doorplate;
import com.my.xq.domain.entity.Proprietor;
import com.my.xq.domain.vo.YwhMember;
import com.my.xq.repository.CourtRepository;
import com.my.xq.repository.CourtRoleBindRepository;
import com.my.xq.repository.CourtRoleRepository;
import com.my.xq.repository.DoorplateRepository;
import com.my.xq.repository.ProprietorRepository;
import com.sskj.core.annotation.ControllerLogExeTime;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/ywhBind")
public class CourtRoleBindServiceImpl {
	@Autowired
	CourtRoleBindRepository courtRoleBindRepository;

	@Autowired
	DoorplateRepository doorplateRepository;

	@Autowired
	ProprietorRepository proprietorRepository;

	@Autowired
	CourtRepository courtRepository;

	@Autowired
	CourtRoleRepository courtRoleRepository;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/addywhBindInfo", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "添加业委会角色信息")
	public void addyzInfo(CourtRole_Proprietor p) {
		courtRoleBindRepository.save(p);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/findAll", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "查询业委会角色信息")
	@ApiOperation(value = "查询业委会角色信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "roleName", value = "姓名", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public Page<CourtRole_Proprietor> findAllPage(@ApiIgnore CourtRoleBindQueryParam pqp, @ApiIgnore Pageable pageable,
			HttpServletRequest request) {
		Page<CourtRole_Proprietor> page = courtRoleBindRepository.findPage(pqp, pageable);
		if (page.getSize() > 0) {
			List<CourtRole_Proprietor> list = page.getContent();
			for (int i = 0; i < list.size(); i++) {
				CourtRole_Proprietor cp = list.get(i);
				Doorplate dp = doorplateRepository.get(cp.getDoorPlateId());
				cp.setDoorplateNo(dp.getDoorplateNo());
				cp.setBuilding(dp.getBuilding());
				cp.setUnit(dp.getUnit());

				String proprietorId = dp.getProprietorId();
				String courtId = dp.getCourtId();
				if (null != proprietorId) {
					cp.setProprietorName(proprietorRepository.get(proprietorId).getName());
				}
				if (null != courtId) {
					cp.setCourtName(courtRepository.get(courtId).getName());
				}
			}
		}
		return page;
	}

	/**
	 * 根据业委会角色获取人员
	 * 
	 * @param courtId
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/member/role", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据角色名称获取业委会人员")
	public List<YwhMember> findYwhGroupByRole(YwhSearchParam param) {
		List<YwhMember> result = new ArrayList<YwhMember>();
		CourtRole role = courtRoleRepository.findCourtRoleByName(param.getRoleName());
		if (role != null && role.getId() != null) {
			List<CourtRole_Proprietor> crps = courtRoleBindRepository.getCortRoleByCourtRoleId(param.getCourtId(),
					role.getId());
			if (crps != null && crps.size() > 0) {
				for (CourtRole_Proprietor crp : crps) {
					Proprietor p = proprietorRepository.findProprietorByDoorId(crp.getDoorPlateId());
					if (p != null) {
						YwhMember member = new YwhMember();
						member.setCourtId(param.getCourtId());
						member.setDoorplateId(crp.getDoorPlateId());
						member.setRoleName(param.getRoleName());
						member.setProprietorId(p.getId());
						member.setProprietorName(p.getName());
						member.setGender(p.getGender());
						member.setPhone(p.getPhone());
						member.setIdentityCard(p.getIdentityCard());
						member.setSynopsis(p.getSynopsis());
						member.setRecord(p.getRecord());
						member.setImageUrl(p.getImageUrl());
						result.add(member);
					}
				}
			}
		}

		return result;
	}

	/**
	 * 根据业委会角色获取人员（主任除外）
	 * 
	 * @param courtId
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/member/role/but/zr", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据角色名称获取业委会人员（主任除外）")
	public List<YwhMember> findYwhGroupByRoleExceptZr(YwhSearchParam param) {
		List<YwhMember> result = new ArrayList<YwhMember>();
		CourtRole role = courtRoleRepository.findCourtRoleByName(param.getRoleName());
		if (role != null && role.getId() != null) {
			List<CourtRole_Proprietor> crps = courtRoleBindRepository.getCortRoleByCourtRoleIdExceptZr(param.getCourtId(),
					role.getId());
			if (crps != null && crps.size() > 0) {
				for (CourtRole_Proprietor crp : crps) {
					Proprietor p = proprietorRepository.findProprietorByDoorId(crp.getDoorPlateId());
					if (p != null) {
						YwhMember member = new YwhMember();
						member.setCourtId(param.getCourtId());
						member.setDoorplateId(crp.getDoorPlateId());
						member.setRoleName(param.getRoleName());
						member.setProprietorId(p.getId());
						member.setProprietorName(p.getName());
						member.setGender(p.getGender());
						member.setPhone(p.getPhone());
						member.setIdentityCard(p.getIdentityCard());
						member.setSynopsis(p.getSynopsis());
						member.setRecord(p.getRecord());
						member.setImageUrl(p.getImageUrl());
						result.add(member);
					}
				}
			}
		}

		return result;
	}

	/**
	 * 根据小区id和业主id获取业主在业委会委员职务
	 * 
	 * @param param
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/member/p/role", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "业委会委员职务获取")
	public CourtRole_Proprietor wyJob(YwhSearchParam param) {
		CourtRole_Proprietor rp = courtRoleBindRepository.getJobByPidAndCid(param.getCourtId(),
				param.getProprietorId());
		if (rp == null) {
			rp = new CourtRole_Proprietor();
		}
		return rp;
	}

	/**
	 * 委员职务角色任命
	 * 
	 * @param param
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/member/appoint", method = { RequestMethod.POST })
	@ControllerLogExeTime(description = "业委会角色任命")
	public boolean appoint(RoleAppointParam param) {
		try {
			YwhSearchParam p = new YwhSearchParam();
			p.setCourtId(param.getCourtId());
			p.setProprietorId(param.getProprietorId());
			CourtRole_Proprietor rp = this.wyJob(p);
			if (rp == null) {
				rp = new CourtRole_Proprietor();
			}
			rp.setDoorPlateId(param.getDoorPlateId());
			rp.setRoleId(param.getRoleId());
			rp.setCourtId(param.getCourtId());
			rp.setRoleName(param.getRoleName());
			courtRoleBindRepository.save(rp);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	/**
	 * 判断业主在某小区是否有某个职务
	 * @param 业主ID
	 * @param 小区ID
	 * @param 角色名称
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/has/role", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "业委会角色判断")
	public boolean hasRole(YwhSearchParam param) {
		List<CourtRole_Proprietor> rps = courtRoleBindRepository.getRole(param.getCourtId(), param.getProprietorId(), param.getRoleName());
		return rps.size() > 0;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "查询一条业委会角色记录", log = false)
	@RequestMapping("/findOne")
	public CourtRole_Proprietor findOne(String id, HttpServletRequest request) {
		CourtRole_Proprietor cp = courtRoleBindRepository.get(id);
		Doorplate dp = doorplateRepository.get(cp.getDoorPlateId());
		cp.setDoorplateNo(dp.getDoorplateNo());
		cp.setBuilding(dp.getBuilding());
		cp.setUnit(dp.getUnit());

		String proprietorId = dp.getProprietorId();
		String courtId = dp.getCourtId();
		if (null != proprietorId) {
			cp.setProprietorName(proprietorRepository.get(proprietorId).getName());
		}
		if (null != courtId) {
			cp.setCourtName(courtRepository.get(courtId).getName());
		}
		return cp;
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@ControllerLogExeTime(description = "删除业委会角色信息", log = false)
	@RequestMapping("/delywhBindInfo")
	public void delyz(String id) {
		courtRoleBindRepository.delete(id);
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/searchywhGroup", method = { RequestMethod.GET })
	@ControllerLogExeTime(description = "根据小区id查询业委会班子")
	public List<CourtRole_Proprietor> searchywhGroup(String courtId) {
		List<CourtRole_Proprietor> list = courtRoleBindRepository.searchywhGroup(courtId);
		for (int i = 0; i < list.size(); i++) {
			CourtRole_Proprietor cp = list.get(i);
			Doorplate dp = doorplateRepository.get(cp.getDoorPlateId());
			cp.setDoorplateNo(dp.getDoorplateNo());
			cp.setBuilding(dp.getBuilding());
			cp.setUnit(dp.getUnit());

			String proprietorId = dp.getProprietorId();
			if (null != proprietorId) {
				cp.setProprietorName(proprietorRepository.get(proprietorId).getName());
			}
			if (null != courtId) {
				cp.setCourtName(courtRepository.get(courtId).getName());
			}
		}
		return list;
	}

	public List<String> toList(String value, String separator) {
		List<String> result = new ArrayList<String>();
		if (!this.isEmpty(value)) {
			String[] as = value.split(separator);
			for (int i = 0; i < as.length; i++) {
				result.add(as[i]);
			}
		}
		return result;
	}

	public boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}
}
